package com.onlyphoto.onlymall.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.onlyphoto.onlymall.R;

import static com.onlyphoto.onlymall.utils.ConvertUtil.dp2px;
import static com.onlyphoto.onlymall.utils.ConvertUtil.sp2px;


/**
 * Created by gaochujia on 2019-7-2
 */
public class OnlyProgressBar extends ProgressBar {

	private static final int DEFAULT_TEXT_SIZE = 10;
	private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;
	private static final int DEFAULT_COLOR_REACHED_COLOR = -500055;
	private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;
	private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;
	private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;
	private static final int DEFAULT_SIZE_TEXT_OFFSET = 0;

	/**
	 * 所有画图所用的画笔
	 */
	protected Paint mPaint = new Paint();

	/**
	 * 进度数字的颜色
	 */
	protected int mTextColor = DEFAULT_TEXT_COLOR;
	/**
	 * 文本的大小
	 */
	protected int mTextSize = sp2px(getContext(), DEFAULT_TEXT_SIZE);

	/**
	 * 进度条文本补偿宽度
	 */
	protected int mTextOffset = dp2px(getContext(), DEFAULT_SIZE_TEXT_OFFSET);

	/**
	 * 完成的进度条高度
	 */
	protected int mReachedProgressBarHeight = dp2px(getContext(), DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);

	/**
	 * 进度条完成的颜色
	 */
	protected int mReachedBarColor = DEFAULT_COLOR_REACHED_COLOR;
	/**
	 * 进度条未完成的颜色
	 */
	protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;
	/**
	 * 未完成的进度条高度
	 */
	protected int mUnReachedProgressBarHeight = dp2px(getContext(), DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);
	/**
	 * 除padding外的视图宽度
	 */
	protected int mRealWidth;

	protected boolean mIfDrawText = false;

	protected static final int visibility = 1;

	public OnlyProgressBar(Context context) {
		this(context, null);
	}
	public OnlyProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	public OnlyProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		obtainStyledAttributes(attrs);
	}

	private void obtainStyledAttributes(AttributeSet attrs){
		final TypedArray attributes = getContext().obtainStyledAttributes(
				attrs, R.styleable.HorizontalProgressBar);
		mReachedBarColor = attributes.getColor(
				R.styleable.HorizontalProgressBar_progress_reached_color, mTextColor);
		mUnReachedBarColor = attributes.getColor(
				R.styleable.HorizontalProgressBar_progress_unreached_color, DEFAULT_COLOR_UNREACHED_COLOR);
		mReachedProgressBarHeight = (int) attributes.getDimension(
				R.styleable.HorizontalProgressBar_progress_reached_bar_height, mReachedProgressBarHeight);
		mUnReachedProgressBarHeight = (int) attributes.getDimension(
				R.styleable.HorizontalProgressBar_progress_unreached_bar_height, mUnReachedProgressBarHeight);
		mTextColor = attributes.getColor(
				R.styleable.HorizontalProgressBar_progress_text_color, DEFAULT_TEXT_COLOR);
		mTextSize = (int) attributes.getDimension(
				R.styleable.HorizontalProgressBar_progress_text_size, mTextSize);
		mTextOffset = (int) attributes.getDimension(
				R.styleable.HorizontalProgressBar_progress_text_offset, mTextOffset);
		int textVisible = attributes.getInt(
				R.styleable.HorizontalProgressBar_progress_text_visibility, visibility);
		if (textVisible == VISIBLE) {
			mIfDrawText = true;
		}
		attributes.recycle();
	}

	protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = measureHeight(heightMeasureSpec);

		setMeasuredDimension(width, height); 									//必须调用该方法来存储View经过测量的到的宽度和高度
		mRealWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft(); //真正的宽度值是减去左右padding
	}

	/**
	 *  EXACTLY：父控件告诉我们子控件了一个确定的大小，按这个大小来布局。如：指定了确定的dp值和macth_parent的情况。
	 *  AT_MOST：当前控件不能超过一个固定的最大值，一般是wrap_content的情况。
	 *  UNSPECIFIED：当前控件没有限制，要多大就有多大，少见。
	 *  @param measureSpec
	 *  @return  视图的高度
	 */
	private int measureHeight(int measureSpec) {

		int result;
		int specMode = MeasureSpec.getMode(measureSpec);			//父布局告诉我们控件的类型
		int specSize = MeasureSpec.getSize(measureSpec);			//父布局传过来的视图大小
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			/**
			 * mPaint.descent() 最高点的高度
			 * mPaint.ascent() 最低点的高度
			 */
			float textHeight = (mPaint.descent() - mPaint.ascent());//设置文本的高度
			/**
			 * Math.abs() 返回绝对值
			 *  Math.max 返回最大值
			 *  Math.min 返回最小值
			 */
			result = (int) (getPaddingTop() + getPaddingBottom() + Math.max(
					Math.max(mReachedProgressBarHeight, mUnReachedProgressBarHeight), Math.abs(textHeight)));
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

	/**
	 * 开始画
	 */
	@Override
	protected synchronized void onDraw(Canvas canvas) {

		canvas.save();
		/**
		 * 设置偏移后的坐标原点 以原来为基础上偏移后， 例如： (100,100), translate(1,1), 坐标原点(101,101);
		 */
		canvas.translate(getPaddingLeft(), getHeight() / 2);
		boolean noNeedBg = false;
		float radio = getProgress() * 1.0f / getMax();				//设置进度
		float progressPosX = (int) (mRealWidth * radio);			//设置当前进度的宽度
		String text = getProgress() + "%";							//设置文本

		float textWidth = mPaint.measureText(text);					//返回文本的宽度
		float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;//设置文本的高度

		//当文本和当前进度的宽度大于bar的宽度时
		if (progressPosX + textWidth > mRealWidth) {
			if (mIfDrawText) {
				progressPosX = mRealWidth - textWidth;
			} else {
				progressPosX = mRealWidth;
			}
			noNeedBg = true;
		}

		//画出bar
		float endX = progressPosX;									//绘制已到达的进度
		//绘制已到达的进度
		if (endX > 0) {
			mPaint.setColor(mReachedBarColor);
			canvas.drawLine(0, 0, endX, 0, mPaint);
		}

		//绘制文本
		if (mIfDrawText) {
			mPaint.setColor(mTextColor);
			mPaint.setTextSize(mTextSize);
			canvas.drawText(text, progressPosX, -textHeight, mPaint);
		}

		//绘制未到达的进度条
		if (!noNeedBg) {
			float start;
			if (mIfDrawText) {
				start = progressPosX + mTextOffset / 2 + textWidth;
			} else {
				start = progressPosX + mTextOffset / 2;
			}
			mPaint.setColor(mUnReachedBarColor);
			mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
			canvas.drawLine(start, 0, mRealWidth, 0, mPaint);
		}
		canvas.restore();
	}
}
